import cv2
import numpy as np
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from matplotlib import pyplot as plt
from PyQt5.QtGui import QIcon
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication
fname=''

class picture(QMainWindow):
    def __init__(self):
        super(picture, self).__init__()
        self.resize(900,500)
        self.center()   #使窗口居中
        self.setWindowIcon(QIcon('E:xiao.jpg'))
        self.setWindowTitle("图片处理")

        self.label = QLabel(self)
        self.label.setText("   图片")
        self.label.setFixedSize(400, 400)
        self.label.move(30,70)
        self.label.setStyleSheet("QLabel{background:white;}"
                                 "QLabel{color:rgb(300,300,300,120);font-size:60px;font-weight:bold;font-family:宋体;}")
        self.label2 = QLabel(self)
        self.label2.setText("   图片")
        self.label2.setFixedSize(400, 400)
        self.label2.move(470, 70)
        self.label2.setStyleSheet("QLabel{background:white;}"
                                 "QLabel{color:rgb(300,300,300,120);font-size:60px;font-weight:bold;font-family:宋体;}")

        b1 = QPushButton(self)
        b1.setText("打开图片")
        b1.resize(180, 30)
        b1.move(30, 30)
        b1.clicked.connect(self.openimage)

        exitAct1 = QAction('&高斯模糊', self)
        exitAct1.triggered.connect(self.mohu)
        exitAct2 = QAction('&滤波', self)
        exitAct2.triggered.connect(self.lvbo)
        exitAct3 = QAction('&直方图', self)
        exitAct3.triggered.connect(self.zhifangtu)
        exitAct4 = QAction('&边缘检测', self)
        exitAct4.triggered.connect(self.bianyuan)

        self.statusBar()

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&操作菜单')
        fileMenu.addAction(exitAct1)
        fileMenu.addAction(exitAct2)
        fileMenu.addAction(exitAct3)
        fileMenu.addAction(exitAct4)

        b2 = QPushButton(self)
        b2.setText("退出")
        b2.resize(400, 30)
        b2.move(470, 30)
        b2.clicked.connect(QCoreApplication.quit)
        print("成功退出")

        b3= QPushButton(self)
        b3.setText("清除图片")
        b3.resize(180,30)
        b3.move(250, 30)
        b3.clicked.connect(self.qingchu)


    def openimage(self):
        global fname
        global one
        global two
        imgName, imgType = QFileDialog.getOpenFileName(self, "打开图片,选择路径需要全英文", "", "*;;*.png;;All Files(*)")
        image=cv2.imread(imgName)
        if image is None:
            print("未选择图片")
        else:
            size = (int(self.label2.width()), int(self.label2.height()))
            shrink = cv2.resize(image, size, interpolation=cv2.INTER_AREA)
            shrink = cv2.cvtColor(shrink, cv2.COLOR_BGR2RGB)
            self.QtImg = QtGui.QImage(shrink.data,
                                      shrink.shape[1],
                                      shrink.shape[0],
                                      QtGui.QImage.Format_RGB888)
            self.label.setPixmap(QtGui.QPixmap.fromImage(self.QtImg))
            self.label2.setPixmap(QtGui.QPixmap.fromImage(self.QtImg))
            fname = imgName
            print(imgName)



    def mohu(self):
        global fname
        global three
        image=cv2.imread(fname)
        if image is None:
            print("未选择图片")
        else:
            dst = cv2.GaussianBlur(image, (0, 0), sigmaX=15)
            size = (int(self.label2.width()), int(self.label2.height()))
            shrink = cv2.resize(dst, size, interpolation=cv2.INTER_AREA)
            shrink = cv2.cvtColor(shrink, cv2.COLOR_BGR2RGB)

            self.QtImg = QtGui.QImage(shrink.data,
                                      shrink.shape[1],
                                      shrink.shape[0],
                                      QtGui.QImage.Format_RGB888)
            self.label2.setPixmap(QtGui.QPixmap.fromImage(self.QtImg))
            cv2.waitKey()
            cv2.destroyAllWindows()


    def lvbo(self):
        global fname
        image = cv2.imread(fname)
        if image is None:
            print("未选择图片")
        else:
            stc = cv2.blur(image, (5, 5))
            size = (int(self.label2.width()), int(self.label2.height()))
            shrink = cv2.resize(stc, size, interpolation=cv2.INTER_AREA)
            shrink = cv2.cvtColor(shrink, cv2.COLOR_BGR2RGB)

            self.QtImg = QtGui.QImage(shrink.data,
                                      shrink.shape[1],
                                      shrink.shape[0],
                                      QtGui.QImage.Format_RGB888)
            self.label2.setPixmap(QtGui.QPixmap.fromImage(self.QtImg))
            cv2.waitKey()
            cv2.destroyAllWindows()


    def zhifangtu(self):
        global fname
        global three
        image = cv2.imread(fname)
        if image is None:
            print("未选择图片")
        else:
            color = ('b', 'g', 'r')
            for i, color in enumerate(color):
                hist = cv2.calcHist([image], [i], None, [256], [0, 256])
                plt.plot(hist, color)
                plt.xlim([0, 256])

            plt.show()
            stc = image
            size = (int(self.label2.width()), int(self.label2.height()))
            shrink = cv2.resize(stc, size, interpolation=cv2.INTER_AREA)
            shrink = cv2.cvtColor(shrink, cv2.COLOR_BGR2RGB)
            self.QtImg = QtGui.QImage(shrink.data,
                                      shrink.shape[1],
                                      shrink.shape[0],
                                      QtGui.QImage.Format_RGB888)
            self.label2.setPixmap(QtGui.QPixmap.fromImage(self.QtImg))


    def bianyuan(self):
        global fname
        image = cv2.imread(fname)
        if image is None:
            print("未选择图片")
        else:
            blur = cv2.GaussianBlur(image, (3, 3), 0)
            stc = cv2.Canny(blur, 50, 150)
            size = (int(self.label2.width()), int(self.label2.height()))
            shrink = cv2.resize(stc, size, interpolation=cv2.INTER_AREA)
            shrink = cv2.cvtColor(shrink, cv2.COLOR_BGR2RGB)
            self.QtImg = QtGui.QImage(shrink.data,
                                      shrink.shape[1],
                                      shrink.shape[0],
                                      QtGui.QImage.Format_RGB888)
            self.label2.setPixmap(QtGui.QPixmap.fromImage(self.QtImg))


    def qingchu(self):
        self.label.setPixmap(QPixmap(""))
        self.label2.setPixmap(QPixmap(""))


    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    my = picture()
    my.show()
    sys.exit(app.exec_())